#include "Problem5.hpp"

Problem5::Problem5(Index dimension) : UnconstrainedProblem(dimension) {
	assert(dimension >= 2);
}

bool Problem5::get_nlp_info(Index& n, Index& nnz_h_lag) {
	n = dimension;
	nnz_h_lag = 0;

	return true;
}

bool Problem5::get_starting_point(Index n, Number* x) {
	for (Index i = 0; i < dimension; ++i) {
		x[i] = 1/(n+1);
	}

	return true;
}

bool Problem5::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) {

	Number sum = 1;
	for (int i = 1; i < n; i++) {
		sum += 100 * (x[i] - x[i-1]*x[i-1])*(x[i] - x[i-1]*x[i-1])+(1-x[i-1])*(1-x[i-1]);
	}
	obj_value = sum;
	return true;

}

bool Problem5::eval_grad_f(Index n, const Number* x, bool new_x,
		Number* grad_f) {


		grad_f[0] = 2*x[0] - 400*x[0]*(x[1] - x[0]*x[0]) - 2;


		grad_f[n-1] = 200*x[n-1] - 200*x[n-2]*x[n-2];

		for (int i = 1; i < n - 1; i++) {
			grad_f[i] =202*x[i] - 200*x[i-1]*x[i-1] - 400*x[i]*(x[i+1] - x[i]*x[i]) - 2;


	}

	return true;
}



	void Problem5::newX(const Number* x) {
	}

